package com.vm.osm.business.controller;

import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.dto.req.checkPlan.ReqSelectTask;
import com.vm.osm.business.dto.req.orderInst.*;
import com.vm.osm.business.dto.rsp.orderInst.RspOrderPage;
import com.vm.osm.business.dto.rsp.orderInst.TaskOrderInfo;
import com.vm.osm.business.service.ICheckPlanInstService;
import com.vm.osm.business.util.UserDataUtil;
import com.vma.eams.spring.boot.annotation.LogFunc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by chenzui on 2018/3/26.
 */
@RestController
@RequestMapping("/v1.0")
@Api(value = "MyLOrderController",description = "我的工单模块--巡检任务",tags = {"MyLOrderController"})
public class MyLoopOrderOperatorController extends ControllerExceptionHandler {

    private Logger LOG = Logger.getLogger(this.getClass());

    @Autowired
    private ICheckPlanInstService checkPlanInstService;

    @ApiOperation(value = "巡检任务接单",notes = "故障单接单")
    @RequestMapping(value = "/check_task/agree_fail",method = RequestMethod.PUT)
    public void agree(HttpServletRequest request,
                      @ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId){
        LOG.info("巡检任务接单");
        checkPlanInstService.agree(orderId, UserDataUtil.getStaffId(request),UserDataUtil.getStaffName(request),UserDataUtil.getStaffHeadImg(request));
    }
    @ApiOperation(value = "巡检任务拒绝接单",notes = "巡检任务拒绝接单")
    @RequestMapping(value = "/check_task/disagree_fail",method = RequestMethod.PUT)
    public void disAgree(@ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId,
                         @ApiParam(value = "type",name="type",required = true) @RequestParam("type") String type,
                         @ApiParam(value = "reason",name="reason",required = true) @RequestParam("reason") String reason,
                         HttpServletRequest request){
        LOG.info("巡检任务拒绝接单");
        String staffId = UserDataUtil.getStaffId(request);
        String staffName = UserDataUtil.getStaffName(request);
        String staffIcon = UserDataUtil.getStaffHeadImg(request);
        checkPlanInstService.disagree(orderId,type,reason,staffId,staffName,staffIcon);
    }



    @ApiOperation(value = "转单接口",notes = "转单接口")
    @ApiImplicitParam(name = "reqTurnOrder",value = "请求参数",required =true,dataType = "ReqTurnOrder")
    @RequestMapping(value = "/check_task/turn",method = RequestMethod.PUT)
    public void turnToOthers(@RequestBody ReqTurnOrder reqTurnOrder, HttpServletRequest request){
        String staffId = UserDataUtil.getStaffId(request);
        String staffName = UserDataUtil.getStaffName(request);
        String headIcon = UserDataUtil.getStaffHeadImg(request);
        reqTurnOrder.setStaff_id(staffId);
        reqTurnOrder.setStaff_name(staffName);
        reqTurnOrder.setHead_icon(headIcon);
        checkPlanInstService.turn(reqTurnOrder);
    }

    @ApiOperation(value = "巡检任务详情",notes = "巡检任务详情")
    @RequestMapping(value = "/loop/detail/{id}",method = RequestMethod.GET)
    public TaskOrderInfo getOrderInfoById(@ApiParam(required=true, name="id", value="系统编号") @PathVariable("id") String id){
        return  checkPlanInstService.getOrderInfo(id);
    }


    @LogFunc(type = "update",name = "巡检完成工单")
    @ApiOperation(value = "更新巡检单KPI信息",notes = "更新巡检单KPI、巡检结果等信息")
    @ApiImplicitParam(name = "reqUpdateLoopTask",value = "reqUpdateLoopTask",required = true,dataType = "ReqUpdateLoopTask")
    @RequestMapping(value = "/loop/update_info",method = RequestMethod.POST)
    public void updateLoopTask(@RequestBody ReqUpdateLoopTask reqUpdateLoopTask,HttpServletRequest request){
        LOG.info("更新巡检单KPI信息");
        checkPlanInstService.updateLoopTask(reqUpdateLoopTask,request);
    }
    @ApiOperation(value = "查询巡检工单",notes = "查询巡检工单")
    @RequestMapping(value = "loop/list",method = RequestMethod.GET)
    public RspOrderPage queryLoops(
            @ApiParam(value = "page_num",name="page_num",required = true) @RequestParam("page_num") Integer pageNum,
            @ApiParam(value = "page_size",name="page_size",required = true) @RequestParam("page_size") Integer pageSize,
            HttpServletRequest request){
//        String staffId = UserDataUtil.getStaffId(request);
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        return checkPlanInstService.queryLoops(pageNum,pageSize,enterpriseId,"");
    }

    @ApiOperation(value = "查询巡检工单",notes = "查询巡检工单")
    @RequestMapping(value = "loop/list2",method = RequestMethod.GET)
    public RspOrderPage queryLoop(
            @RequestBody ReqSelectTask reqSelectTask,
            HttpServletRequest request){
        return checkPlanInstService.queryLoop(reqSelectTask,request);
    }

}
